package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.entity.OmPosition;
import com.augurit.common.entity.OmUserOrg;
import com.augurit.common.entity.OmUserPosition;
import com.augurit.common.system.convert.OmUserPositionConverter;
import com.augurit.common.system.web.org.form.OmUserPositionForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;

/**
 * 用户岗位关联DAO类
 */
@Repository
public class OmUserPositionDao extends BaseDao<OmUserPosition, Long> {

	
	@Autowired
	private OmPositionDao omPositionDao;
	
	@Autowired
	private OmUserOrgDao omUserOrgDao;
	
	/**
	 * 保存用户岗位关联Form对象
	 * @param form 用户岗位关联Form对象
	 */
	public void save(OmUserPositionForm form){
		
		if(form != null){
			OmUserPosition entity = null;
			
			//准备VO对象
			if(form != null && form.getUpId() != null){
				entity = this.get(form.getUpId());
			}else{
				entity = new OmUserPosition();
			}
			
			//属性值转换
			OmUserPositionConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setUpId(entity.getUpId());
		}
	}
	
	/**
	 * 判断用户是否已经拥有指定岗位
	 * @param userId
	 * @param posId
	 * @return
	 */
	public boolean isUserHavePos(Long userId, Long posId){
		OmUserPosition entity = this.get(userId, posId);
		if(entity == null)
			return false;
		return true;
	}
	
	/**
	 * 根据用户ID查询关联岗位ID列表
	 * @param userId 用户ID
	 * @return 关联岗位ID列表
	 */
	public List<Long> getPostionIds(Long userId){
		return partSelectOmUserPosition(userId, "posId");
	}
	
	/**
	 * 根据用户ID查询关联OmUserPosition对象列表
	 * @param userId 用户ID
	 * @return OmUserPosition对象列表
	 */
	public List<OmUserPosition> getByUserId(Long userId){
		return partSelectOmUserPosition(userId, null);
	}
	
	private List partSelectOmUserPosition(Long userId, String entityProperty){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from OmUserPosition ps where ps.userId=?";
		List values = new ArrayList();
		values.add(userId);
		
		return this.find(hql,values);
	}
	
	/**
	 * 根据用户ID删除对象
	 * @param userId 用户ID
	 */
	public void deleteByUserId(Long userId){
		List<OmUserPosition> list = this.getByUserId(userId);
		if(list != null && list.size() > 0){
			this.delete(list);
		}
	}
	
	/**
	 * 根据岗位ID查询关联OmUserPosition对象列表
	 * @param posId 岗位ID
	 * @return 关联OmUserPosition对象列表
	 */
	public List<OmUserPosition> getByPosId(Long posId){
		if(posId != null){
			return this.findBy("posId",posId);
		}else
			return null;
	}
	
	/**
	 * 根据posId获取关联的userId集合
	 * @param posId 用户原来所在的岗位
	 * @return  list 与岗位关联的userId集合
	 * */
	public List<Long> getUserIdsByPosId(Long posId){
		if(posId != null){
			String hql =" select distinct ps.userId from OmUserPosition ps where ps.posId=?";
			List<Long> values = new ArrayList<Long>();
			values.add(posId);
			return this.find(hql, values);
		}else
			return null;
	}
	
	/**
	 * 根据指定岗位数组获取对应的用户id数组
	 * @param posIds 岗位id数组
	 * @return 用户id数组
	 * @author 王剑文
	 */
	public Long[] getUserIdsByPosIds(Long[] posIds){
		if(posIds != null && posIds.length > 0){
			String hql = "select distinct ps.userId from OmUserPosition ps where " + HqlUtils.buildOrCondition("ps", "posId", posIds);
			List<Long> list = this.find(hql);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}

	/**
	 * 根据岗位ID删除对象
	 * @param posId 岗位ID
	 */
	public void deleteByPosId(Long posId){
		List<OmUserPosition> list = this.getByPosId(posId);
		if(list != null && list.size() > 0){
			this.delete(list);
		}
	}
	
	/**
	 * 根据用户ID和岗位ID查找OmUserPosition对象
	 * @param userId 用户ID
	 * @param posId 岗位ID
	 * @return
	 */
	public OmUserPosition get(Long userId, Long posId){
		if(userId != null && posId != null){
			String hql = "from OmUserPosition ps where ps.userId=? and ps.posId=?";
			List values = new ArrayList();
			values.add(userId);
			values.add(posId);
			List<OmUserPosition> entities = this.find(hql, values);
			if(entities != null && entities.size() == 1){
				return entities.get(0);
			}
		}
		return null;
	}

	/**
	 * 根据用户ID和岗位ID保存一个OmUserOrgForm对象
	 * @param userId 用户ID
	 * @param posId 岗位ID
	 */
	public void save(Long userId, Long posId){
		if(userId != null && posId != null){
			OmUserPosition entity = new OmUserPosition();
			entity.setUserId(userId);
			entity.setPosId(posId);
			entity.setIsMain(OrgConstant.ZERO);
			this.save(entity);
		}
	}
	
	/**
	 * 移动用户从原岗位到目标岗位
	 * @param userId 用户主键
	 * @param oldPosId 用户原来所在的岗位
	 * @param targetPosId 要移动到的岗位主键
	 * */
	public void moveUserToOtherPosition(Long userId, Long oldPosId, Long targetPosId){
		if(userId != null && oldPosId != null && targetPosId != null){
			//判断两个岗位是否属于同一机构
			OmPosition oldPosition = omPositionDao.get(oldPosId);
			OmPosition targetPosition = omPositionDao.get(targetPosId);
			if(oldPosition.getPartyId().longValue() != targetPosition.getPartyId().longValue()){
				//删除与旧机构的关联
				OmUserOrg oldOmUserOrg = omUserOrgDao.get(userId, oldPosition.getPartyId());
				
				//添加与新机构的关联
				OmUserOrg targetOmUserOrg = new OmUserOrg();
				targetOmUserOrg.setUserId(userId);
				targetOmUserOrg.setOrgId(targetPosition.getPartyId());
				omUserOrgDao.save(targetOmUserOrg);
			}
			
			//删除原来的记录userId和oldPosId对应的一个OmUserPositionForm对象
			OmUserPosition entity = this.get(userId, oldPosId);
			if(entity != null)
				this.delete(entity);
			
			//增加新的对象
			this.save(userId, targetPosId);	
		}
	}
	
	/**
	 * 移动无岗位用户到目标岗位
	 * @param userId 用户主键
	 * @param oldPosId 用户原来所在的岗位
	 * @param targetPosId 要移动到的岗位主键
	 * */
	public void moveUserToPosition(Long userId, Long targetPosId){
		if(userId != null && targetPosId != null){
			//增加新的对象
			this.save(userId, targetPosId);
		}
	}
	
	/**
	 * 设置主岗位
	 * @param userId 用户ID
	 * @param posId 岗位ID
	 */
	public void setMainPos(Long userId, Long posId){
		if(userId != null && posId != null){
			//获取指定用户关联OmUserOrg对象列表
			List<OmUserPosition> list = this.getByUserId(userId);
			
			if(list != null && list.size() > 0){
				for(OmUserPosition entity : list){
					
					if(entity.getPosId().longValue() == posId.longValue())
						entity.setIsMain(OrgConstant.ONE);
					else
						entity.setIsMain(OrgConstant.ZERO);
					
					this.save(entity);
				}
			}
		}
	}
	
	/**
	 * 删除用户与岗位的关联，不删除用户本身
	 * @param userId 用户主键
	 * @param posId 岗位主键
	 */
	public void deleteUserFromPos(Long userId, Long posId){
		// 删除用户关联的岗位
		if(userId != null && posId != null){
			String hql = "from OmUserPosition ps where userId = ? and posId = ?";
			List<Long> values = new ArrayList<Long>();
			values.add(userId);
			values.add(posId);
			
			OmUserPosition entity = (OmUserPosition)this.findUnique(hql, values);
			this.delete(entity);
		}
	}
}
